#include "history.h"
#include "ui_history.h"

History::History(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::History)
{
    ui->setupUi(this);

    connect(ui->btnSelect,SIGNAL(clicked()),
            this,SLOT(btnSelectSlot()));
}

History::~History()
{
    delete ui;
}

void History::selectAll()
{
    QString sql = "SELECT * FROM group_history";
    QSqlQuery sq;
    if(sq.exec(sql))
    {
        // 清空上次显示
        ui->textBrowser->clear();
        while (sq.next()) // 向后移动
        {
            QString time = sq.value("time").toString(); // 按照序号取出
            QString name = sq.value("name").toString(); // 按照名字取出
            QString message = sq.value("message").toString();
            // 组装
            QString space = "  ";
            QString record = time + space + name + space + message;
            // 展示
            ui->textBrowser->append(record);
        }
    }
    else
    {
        // 获得错误信息对象
        QSqlError info = sq.lastError();
        // 获得错误信息文本
        QString text = info.text();
        QMessageBox::warning(this,"通知",text);
    }
}

void History::selectPrecise()
{
    if(ui->comboBox->currentText()== "全部")
    {
        selectAll();
    }
    else if(ui->comboBox->currentText()== "关键字")
    {
        QString word = ui->lineEdit->text();
        if(word == "")
        {
            QMessageBox::warning(this,"提示","请输入查询内容！");
            return;
        }
        QString sql = "SELECT * FROM group_history WHERE message LIKE ?";
        QSqlQuery sq;
        sq.prepare(sql);
        sq.addBindValue(word.prepend("%").append("%")); // 前后加通配符
        if(sq.exec())
        {
            // 清空上次显示
            ui->textBrowser->clear();
            while(sq.next()) // 向后移动
            {
                QString time = sq.value("time").toString(); // 按照序号取出
                QString name = sq.value("name").toString(); // 按照名字取出
                QString message = sq.value("message").toString();
                // 组装
                QString space = "  ";
                QString record = time + space + name + space + message;
                // 展示
                ui->textBrowser->append(record);
            }
        }
        else
        {
            // 获得错误信息对象
            QSqlError info = sq.lastError();
            // 获得错误信息文本
            QString text = info.text();
            QMessageBox::warning(this,"通知",text);
        }
    }
    else if(ui->comboBox->currentText()== "日期")
    {
        QString dt = ui->lineEdit->text();
        if(dt == "")
        {
            QMessageBox::warning(this,"提示","请输入查询内容！");
            return;
        }
        QString sql = "SELECT * FROM group_history WHERE time LIKE ?";
        QSqlQuery sq;
        sq.prepare(sql);
        sq.addBindValue(dt.prepend("%").append("%")); // 前后加通配符
        if(sq.exec())
        {
            // 清空上次显示
            ui->textBrowser->clear();
            while(sq.next()) // 向后移动
            {
                QString time = sq.value("time").toString(); // 按照序号取出
                QString name = sq.value("name").toString(); // 按照名字取出
                QString message = sq.value("message").toString();
                // 组装
                QString space = "  ";
                QString record = time + space + name + space + message;
                // 展示
                ui->textBrowser->append(record);
            }
        }
        else
        {
            // 获得错误信息对象
            QSqlError info = sq.lastError();
            // 获得错误信息文本
            QString text = info.text();
            QMessageBox::warning(this,"通知",text);
        }
    }
}

void History::btnSelectSlot()
{
    selectPrecise();
}
